package com.jingbian.study.service.Impl;/**
 * @Auter 李孝在
 * @Date 2025/1/15
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingbian.study.entity.Jobs;
import com.jingbian.study.mapper.JobsMapper;
import com.jingbian.study.resp.ResultData;
import com.jingbian.study.service.IJobService;
import groovy.lang.GroovyClassLoader;
import jakarta.annotation.Resource;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * @projectName: study
 * @package: com.jingbian.study.service.Impl
 * @className: JobService
 * @author: Eric
 * @description: TODO
 * @date: 2025/1/15 21:22
 * @version: 1.0
 */
@Service
public class JobServiceImpl implements IJobService {

    private final JobsMapper jobsMapper;
    private final GroovyClassLoader classLoader;
    private final Scheduler scheduler;

    @Autowired
    public JobServiceImpl(JobsMapper jobsMapper,GroovyClassLoader classLoader,Scheduler scheduler){
        this.jobsMapper=jobsMapper;
        this.classLoader=classLoader;
        this.scheduler=scheduler;
    }

    @Override
    public void saveJob(Jobs jobs) {
        jobs.setId(0);
        jobsMapper.insert(jobs);
    }

    @Override
    public void deleteJob(Integer id) {
        jobsMapper.deleteById(id);
    }

    @Override
    public void updateJob(Jobs jobs) {
        jobsMapper.updateById(jobs);
    }

    @Override
    public List<Jobs> gitJobList() {
        return jobsMapper.selectList(null);
    }

    @Override
    public Jobs getJob(Integer id) {
        return jobsMapper.selectById(id);
    }

    @Override
    public void test() {
        System.out.println("test");
    }

    @Override
    public String installJob(Integer jobId,String triggerName,String triggerGroup) {
        Jobs jobs = getJob(jobId);
        try {
            Class<? extends Job> clazz = classLoader.parseClass(jobs.getCode());
            // 确保动态加载的类实现了 Job 接口
            if (!Job.class.isAssignableFrom(clazz)) {
                return ("提供的脚本没有实现 Job 接口。");
            }

            // 使用动态加载的类创建 JobDetail
            JobDetail job = JobBuilder.newJob(clazz)
                    .withIdentity(jobs.getJobName(), jobs.getJobGroup())
                    .build();

            // 定义一个每 10 秒触发一次的触发器
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerName, triggerGroup)
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(10)
                            .repeatForever())
                    .build();

            // 使用调度器调度任务
            scheduler.scheduleJob(job, trigger);

            return "任务启动成功。";
        }catch (RuntimeException | SchedulerException e){
            return "任务启动失败,是否替换任务。";
        }
    }

    @Override
    public void unloadJob(Integer jobId, String triggerName, String triggerGroup) throws SchedulerException {
        Jobs jobs = getJob(jobId);
        JobKey jobKey = new JobKey(jobs.getJobName(), jobs.getJobGroup());
        TriggerKey triggerKey = new TriggerKey(triggerName,triggerGroup);

        // 取消触发器
//        scheduler.unscheduleJob(triggerKey);
        //删除任务
        scheduler.deleteJob(jobKey);
    }
}
